package org.thingsboard.server.remote.upload.gateway;
import lombok.Data;
import org.thingsboard.server.common.data.kv.*;
import org.thingsboard.server.remote.upload.AttributesUpdateListener;
import org.thingsboard.server.remote.upload.data.AttributesUpdateSubscription;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.UUID;
import java.util.stream.Collectors;

@Data
public class DeviceDemo {
    private String name;
    private String type;

    public List<TsKvEntry> getAffectedTimeseries() {
        List<KvEntry> result = new ArrayList<>();
        Random random=new Random();
        result.add(new LongDataEntry("status", random.nextLong()));
        result.add(new StringDataEntry("uuid", UUID.randomUUID().toString()));
        return  result.stream()
                .map(kv -> new BasicTsKvEntry(System.currentTimeMillis(), kv))
                .collect(Collectors.toList());
    }

    public AttributesUpdateSubscription attributesUpdateSubscription(String name) {
        DeviceAttributesUpdateListener attributesUpdateListener=new DeviceAttributesUpdateListener();
        return new AttributesUpdateSubscription(name,attributesUpdateListener);
    }

    static class DeviceAttributesUpdateListener implements AttributesUpdateListener {

       @Override
       public void onAttributesUpdated(String deviceName, List<KvEntry> attributes) {
           attributes.forEach(kvEntry->{
               System.out.println("更新设备属性【"+kvEntry+"】");
           });
       }
   }
}
